var app = getApp();
Page( Object.assign({},{
    data:{
        article : {},
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        fucus : false,
        userinfo : {},
        content : '',
        page : {
            isend : false,
            doing : false,
            doo : 'article',
            pdata : {
                op : 'replylist',
                page : 1,
                type : 0,
                aid : '',
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        timer : '',
    },
    onLoad:function(options){
        var self = this;

        if( options.scene ){
            var list = options.scene.split('_');
            options.id = list[0] ? list[0] : 0;
            options.zfid = list[1] ? list[1] : 0;
        }
        
        var id = options.id ? options.id : '';
        var zfid = options.zfid ? options.zfid : 0;
        if(options.scene) id = options.scene;

        self.setData({
            options : options,
        })
        self.data.page.pdata.page = 1;
        self.data.page.waitf = 0;
        self.data.page.nodataf = 0;
        self.data.page.isend = false;
        self.data.page.doing = false;
        self.data.datalist = [];
        app.com.auth(function(user){
            app.com.http('article','POST',{op:'info',id:id,zfid:zfid},0,false,'','',function(res){
                if (!res.data.errno && res.data) {
                    self.setData({
                        article : res.data.data.article,
                        params : res.data.data.params,
                        sharedata : res.data.data.sharedata,
                        userinfo : res.data.data.userinfo,
                        inited : true,
                    });

                    self.data.page.pdata.aid = res.data.data.article.id;
                    self.onReachBottom();

                    if( res.data.data.content ){
                        setTimeout(function(){
                            var octoparse = require('../resource/wxParse/html2json2.js');
                            var transData = octoparse.parse(res.data.data.content);                   
                            self.setData({
                                nodes : transData,
                            })
                        },5)
                    }

                    if(res.data.data.article.isgivejf == 1){
                        self.data.timer = setTimeout(function(){
                            self.givejf();
                        },8000)
                    }

                    wx.setNavigationBarTitle({
                        title: res.data.data.article.title
                    });

                }else{
                    app.com.alert(res.data.message,function(){
                        app.com.backPage(0);
                    });
                }
            });
        })
    },
    onUnload(){
        if(this.data.timer){
            clearTimeout(this.data.timer);
        }
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onShareTimeline(){
        return {
            title: this.data.sharedata.title,
            query: this.data.sharedata.query,
            imageUrl : this.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
        wx.stopPullDownRefresh();
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,0,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    givejf(){
        var self = this;
        var postdata = {
            op : 'getartjf',
            id : self.data.article.id,
        };
        app.com.http('article','POST',postdata,0,false,'','',function(res){
            if (!res.data.errno && res.data) {
                if(res.data.message) app.com.toast(res.data.message);
            }
        });
    },
    likeit(e){
        var self = this;
        var i = e.currentTarget.dataset.i;
        var postdata = {
            op : 'likeit',
            id : self.data.article.id,
            iid : e.currentTarget.dataset.id,
            type : e.currentTarget.dataset.type,
        };
        app.com.http('article','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {

                if(postdata.type == 1){
                    
                    let num = self.data.datalist[i].zan*1 + 1;
                    self.setData({
                        ['datalist['+i+'].zan'] : num,
                    })
                }else{
                    let num = self.data.article.zan*1 + 1;
                    self.setData({
                        'article.zan' : num,
                    })
                }
                app.com.toast(res.data.message);
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    admindeal(e){
        var self = this;
        var postdata = {
            op : 'admindeal',
            id : self.data.article.id,
            iid : e.currentTarget.dataset.id,
            type : e.currentTarget.dataset.type,
        };
        var i = e.currentTarget.dataset.i;

        app.com.http('article','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                app.com.toast(res.data.message);
                if(postdata.type*1 == 1){
                    self.setData({
                        ['datalist['+i+'].status'] : 1,
                    })
                }else{
                    self.setData({
                        ['datalist['+i+'].status'] : 0,
                    })
                }
            }else{
                app.com.alert(res.data.message);
            }
        });
        
    },
    submess(){
        var self = this;
        var postdata = {
            op : 'submess',
            id : self.data.article.id,
            content : self.data.content,
        };
        app.com.confirm('确定提交吗？',function(){
            app.com.http('article','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno && res.data) {
                    app.com.toast(res.data.message);
                    self.setData({
                        content : '',
                    })
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });
    },
    inputmess(e){
        this.setData({
            content : e.detail.value,
        })
    },
    focusfn(){
        this.setData({
            fucus : true,
        })
    },
    blurfn(){
        this.setData({
            fucus : false,
        })
    },
    shareit(){
        this.selectComponent('#share').onClose();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e);
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    weburl : function(e){
        app.com.toweburl( e,this );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    
}))